package com.eb.buybuybuy.mapper;

import com.eb.buybuybuy.annotation.Component;
import com.eb.buybuybuy.bean.User;
import com.eb.buybuybuy.util.MysqlUtil;

import java.sql.SQLException;
import java.util.List;

@Component
public class UserMapper {
    public User getById(Integer id) throws Exception {
        List<User> list = MysqlUtil.query("select * from user where id = ?", User.class, id);
        return list.isEmpty() ? null : list.get(0);
    }

    public User getByUsername(String username) throws Exception {
        List<User> list = MysqlUtil.query("select * from user where username = ?", User.class, username);
        return list.isEmpty() ? null : list.get(0);
    }

    public void insert(User user) throws Exception {
        MysqlUtil.update("insert into user(username, password) values(?, ?)", user.getUsername(), user.getPassword());
    }
}
